package com.yihu.wlyy.service.app.health;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.yihu.wlyy.util.DateUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.yihu.wlyy.entity.patient.PatientHealthRecordDiet;
import com.yihu.wlyy.entity.patient.PatientHealthRecordMedication;
import com.yihu.wlyy.entity.patient.PatientHealthRecordSports;
import com.yihu.wlyy.repository.PatientHealthRecordDietDao;
import com.yihu.wlyy.repository.PatientHealthRecordMedicationDao;
import com.yihu.wlyy.repository.PatientHealthRecordSportsDao;
import com.yihu.wlyy.repository.SportsDao;
import com.yihu.wlyy.repository.SportsTypeDao;
import com.yihu.wlyy.service.BaseService;

@Component
@Transactional(rollbackFor = Exception.class)
public class PatientHealthRecordService extends BaseService {

	@Autowired
	private SportsTypeDao sportsTypeDao;

	@Autowired
	private SportsDao sportsDao;

	@Autowired
	private PatientHealthRecordMedicationDao patientHealthRecordMedicationDao;

	@Autowired
	private PatientHealthRecordDietDao patientHealthRecordDietDao;

	@Autowired
	private PatientHealthRecordSportsDao patientHealthRecordSportsDao;

	/**
	 * 添加运动记录
	 * @param record 运动记录对象
	 * @return
	 */
	public PatientHealthRecordSports addSports(PatientHealthRecordSports record, String sportsType, String sports) {
		record.setCode(getCode());
		record.setSportsType(sportsType);
		record.setSportsTypeName(sportsTypeDao.findByCode(sportsType).getName());
		record.setSports(sports);
		record.setSportsName(sportsDao.findByCode(sports).getName());
		return patientHealthRecordSportsDao.save(record);
	}

	/**
	 * 添加用药记录
	 * @param record 用药记录对象
	 * @return
	 */
	public PatientHealthRecordMedication addMedication(PatientHealthRecordMedication record) {
		record.setCode(getCode());
		return patientHealthRecordMedicationDao.save(record);
	}

	/**
	 * 添加饮食记录
	 * @param record 饮食记录对象
	 * @return
	 */
	public PatientHealthRecordDiet addDiet(PatientHealthRecordDiet record) {
		record.setCode(getCode());
		return	 patientHealthRecordDietDao.save(record);
	}

	/**
	 * 按分类查询患者运动记录
	 * @param patient 患者标识
	 * @param page 页码
	 * @param pageSize 分页大小
	 * @return
	 */
	public Page<PatientHealthRecordSports> findSportsByPatient(String patient, Date sortDate, int pageSize) {
		if (pageSize <= 0) {
			pageSize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "sortDate");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pageSize, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		// 患者标志
		filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
		if(sortDate != null){
			filters.put("sortDate", new SearchFilter("sortDate", Operator.LT, sortDate));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<PatientHealthRecordSports> spec = DynamicSpecifications.bySearchFilter(filters.values(), PatientHealthRecordSports.class);
		return patientHealthRecordSportsDao.findAll(spec, pageRequest);
	}

	/**
	 * 查询运动记录
	 *
	 * @param patient
	 * @param start
	 * @param end
	 * @param page
	 * @param pagesize
	 * @return
	 */
	public Page<PatientHealthRecordSports> findSportsByPatientPage(String patient,String start,String end,int page,int pagesize){
		if(page > 0) {
			page = page -1;
		}
		PageRequest pageRequest = new PageRequest(page,pagesize);

		return patientHealthRecordSportsDao.findSportsByPatient(patient, DateUtil.strToDate(start,DateUtil.YYYY_MM_DD_HH_MM_SS)
				,DateUtil.strToDate(end,DateUtil.YYYY_MM_DD_HH_MM_SS),pageRequest);
	}

	/**
	 * 查询用药记录
	 *
	 * @param patient
	 * @param start
	 * @param end
	 * @param page
	 * @param pagesize
	 * @return
	 */
	public Page<PatientHealthRecordMedication> findMedicalByPatientPage(String patient,String start,String end,int page,int pagesize){
		if(page > 0)
		{
			page = page -1;
		}
		PageRequest pageRequest = new PageRequest(page,pagesize);
		return patientHealthRecordMedicationDao.findMedicalByPatient(patient, DateUtil.strToDate(start,DateUtil.YYYY_MM_DD_HH_MM_SS)
				,DateUtil.strToDate(end,DateUtil.YYYY_MM_DD_HH_MM_SS),pageRequest);
	}

	/**
	 * 查询饮食记录
	 *
	 * @param patient
	 * @param start
	 * @param end
	 * @param page
	 * @param pagesize
	 * @return
	 */
	public Page<PatientHealthRecordDiet> findDietByPatientPage(String patient,String start,String end,int page,int pagesize){
		if(page > 0) {
			page = page -1;
		}
		PageRequest pageRequest = new PageRequest(page,pagesize);
		return patientHealthRecordDietDao.findDietByPatient(patient, DateUtil.strToDate(start,DateUtil.YYYY_MM_DD_HH_MM_SS)
				,DateUtil.strToDate(end,DateUtil.YYYY_MM_DD_HH_MM_SS),pageRequest);
	}

	/**
	 * 按分类查询患者用药记录
	 * @param patient 患者标识
	 * @param pageSize 分页大小
	 * @return
	 */
	public Page<PatientHealthRecordMedication> findMedicationByPatient(String patient, Date sortDate, int pageSize) {
		if (pageSize <= 0) {
			pageSize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "sortDate");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pageSize, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		// 患者标志
		filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
		if(sortDate != null){
			filters.put("sortDate", new SearchFilter("sortDate", Operator.LT, sortDate));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<PatientHealthRecordMedication> spec = DynamicSpecifications.bySearchFilter(filters.values(), PatientHealthRecordMedication.class);
		return patientHealthRecordMedicationDao.findAll(spec, pageRequest);
	}



	/**
	 * 按分类查询患者饮食记录
	 * @param patient 患者标识
	 * @param page 页码
	 * @param pageSize 分页大小
	 * @return
	 */
	public Page<PatientHealthRecordDiet> findDietByPatient(String patient, Date sortDate, int pageSize) {
		if (pageSize <= 0) {
			pageSize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "sortDate");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pageSize, sort);
		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		// 患者标志
		filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
		if(sortDate != null){
			filters.put("sortDate", new SearchFilter("sortDate", Operator.LT, sortDate));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<PatientHealthRecordDiet> spec = DynamicSpecifications.bySearchFilter(filters.values(), PatientHealthRecordDiet.class);
		return patientHealthRecordDietDao.findAll(spec, pageRequest);
	}

	/**
	 * 查询患者最近填写的运动、用药、饮食内容
	 * @param patient
	 * @return
	 */
	public JSONObject findRecentByPatient(String patient) {
		JSONObject jsonObject = new JSONObject();
		// 查询最近的运动
		Page<PatientHealthRecordSports> sports = patientHealthRecordSportsDao.findRecentByPatient(patient, new PageRequest(0, 1));
		// 查询最近的用药
		Page<PatientHealthRecordMedication> medication = patientHealthRecordMedicationDao.findRecentByPatient(patient, new PageRequest(0, 1));
		// 查询最近的饮食
		Page<PatientHealthRecordDiet> diet = patientHealthRecordDietDao.findRecentByPatient(patient, new PageRequest(0, 1));
		if (sports != null && sports.getSize() > 0) {
			for (PatientHealthRecordSports temp : sports) {
				jsonObject.put("sports", temp.getSportsName());
			}
		}
		if (medication != null) {
			for (PatientHealthRecordMedication temp : medication) {
				jsonObject.put("medication", temp.getMedicinesName());
			}
		}
		if (diet != null) {
			for (PatientHealthRecordDiet temp : diet) {
				jsonObject.put("diet", temp.getContent());
			}
		}
		return jsonObject;
	}

}
